<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.caseName"
                        placeholder="案例名称"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Select
                        style="width: 140px;"
                        placeholder="类型"
                        v-model="params.caseType"
                        clearable
                    >
                        <Option :value="1">一般贸易进出口</Option>
                        <Option :value="2">保税仓进出口</Option>
                        <Option :value="3">其他类型进出口</Option>
                    </Select>
                </FormItem>
                <FormItem v-if="roleType!=2">
                    <Select
                        style="width: 140px;"
                        placeholder="来源"
                        v-model="params.schoolId"
                        clearable
                    >
                        <Option
                            v-for="(item,index) in schoolList"
                            :key="index"
                            :value="item.id"
                        >{{item.name}}</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAdd" >新建案例</Button>
            </div>
        </div>
        <Table :data="list" :columns="columns" border align="center">
            <template slot-scope="{ row }" slot="caseType">
                <div>{{ row.caseType==1?'一般贸易进出口':row.caseType==2?'保税仓进出口':row.caseType==3?'其他类型进出口':'' }}</div>
            </template>
            <template slot-scope="{ row }" slot="caseName">
                <div>{{ row.caseName }}</div>
            </template>
            <template slot-scope="{ row }" slot="creatorName">
                <div>{{ row.creatorName }}</div>
            </template>
            <template slot-scope="{ row }" slot="createTime">
                <div>{{ row.createTime }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button type="text" @click="handleView(row.caseId)">预览</Button>
                    <Button type="text" @click="handleDownload(row.caseId)">下载</Button>
                    <Button type="text" :disabled="(roleType==2&&1==row.roleType)||(roleType==1&&2==row.roleType)" @click="handleEdit(row.caseId)">编辑</Button>
                    <Button type="text" :disabled="(roleType==2&&1==row.roleType)||(roleType==1&&2==row.roleType)" @click="handleDelete(row.caseId)">删除</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { queryCaseList, caseDelete } from '@/api/caseManage'
import { schoolManageList } from '@/api/schoolManage'
import { downloadDoc } from '@/api/fileManage'

export default {
    inject: ['nextPageReload'],
    data () {
        return {
            roleType: '',
            schoolList: [],
            params: {
                caseName: '',
                caseType: '',
                schoolId: '',
                pageNum: 1,
                pageSize: 10,
                total: 0
            },
            list: [],
            columns: Object.freeze([
                { title: '序号', type: 'index', align: 'center', width: 80 },
                // { title: '案例类型', slot: 'caseType', align: 'center', width: 200 },
                { title: '类型', slot: '', align: 'center', width: 200 },
                { title: '分类', slot: '', align: 'center', width: 200 },
                { title: '案例名称', slot: 'caseName', align: 'center' },
                { title: '来源', slot: 'creatorName', align: 'center' },
                { title: '创建时间', slot: 'createTime', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 200 }
            ])
        }
    },
    mounted () {
        this.queryList()
        this.getSchoolList()
        this.getRoleType()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        getRoleType () {
            let info = JSON.parse(window.localStorage.getItem('userinfo'))
            this.roleType = info.roleType
        },
        getSchoolList () {
            var obj = { 'total': 0, 'status': '', 'pageNum': 1, 'pageSize': 10, 'schoolName': '', 'enable': '' }
            schoolManageList(obj).then(res => {
                console.log(res.data.data.list)
                this.schoolList = res.data.data.list.map(item => {
                    return {
                        id: item.schoolId,
                        name: item.schoolName
                    }
                })
                this.schoolList.push({ id: 1, name: '系统后台' })
            })
        },
        clearSearch () {
            this.params.caseName = ''
            this.params.caseType = ''
            this.params.schoolId = ''
            this.params.pageNum = 1
            this.queryList()
        },
        queryList () {
            queryCaseList(this.params).then(res => {
                if (res.data.code === '0000') {
                    this.list = res.data.data.list
                    this.params.total = res.data.data.total
                }
            })
        },
        handleView (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'caseManageView',
                query: {
                    id: id
                }
            })
        },
        handleAdd () {
            this.nextPageReload()
            this.$router.push({
                name: 'caseManageAdd'
            })
        },
        handleEdit (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'caseManageEdit',
                params: {
                    id: id
                }
            })
        },
        handleDelete (id) {
            // console.log(this.store.state.user.roleType)
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否删除？',
                onOk: () => {
                    caseDelete(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success('删除成功')
                            this.params.pageNum = 1
                            this.queryList()
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        },
        handleDownload (id) {
            downloadDoc(id, '/case/download')
        }
    }
}
</script>
